<?php
	
	if(isset($_POST['page'])) {
		$pag = $_POST['page'];
		include('../inc/fnc.php');
		include('../inc/constants.php');
	} else{
		$pag = 1;
	}
	
	//If ajax call, include the proper files 
	if ($_GET['ajax'] == 'true' && (!isset($_POST['page'])) ) { 
		include('../inc/fnc.php');
		include('../inc/constants.php');
	}
	
	session_start();
    $hotarray = array();
    $hotarray = findlatest($pag);
	
	$return = "";
    foreach($hotarray as $k => $v) {
        $showmore = 0;
        $srno = ($pag * STORYINPAGE - STORYINPAGE) + 1;
	
        $query = mysql_query("SELECT story.storyname as stitle  FROM story join storyline on story.storyid = storyline.storyid WHERE story.storyid = {$k} and storyline.delflag = 'N'");
        while($row = mysql_fetch_array($query)) {
            $stitle = $row['stitle'];
        }
        
     $linecount = mysql_num_rows($query);
        if($linecount > 5) {
            $maxlimit = $linecount;
            $linecount = $linecount - 5;
            $showmore = 1;
        }

		$stitle = ucfirst($stitle);	
        
		$return .= "<div class='topShadow'></div><div class='story'>";
		$return .="<h1><a href='index.php?page=story&id={$k}&title=".getStoryTitle($k)."'>{$stitle}</a></h1> 
        			<ul class='timeline'>";
					
						$query1 = "SELECT * FROM storyline WHERE storyid = {$k} and delflag = 'N'";
						if($showmore == 1) { $query1 .= " LIMIT 5"; }
						$result = mysql_query($query1);
						while($rows = mysql_fetch_array($result)) {
							$queryTime = "SELECT crtdtime FROM storyline WHERE storylineid = {$rows['storylineid']} AND storyid = {$k} AND delflag = 'N'";
							$resultTime = mysql_query($queryTime);
							while($rowTime = mysql_fetch_array($resultTime)) {
								$showTime = timeago($rowTime['crtdtime']);
							}
							$credits = userlabel($rows['crtdbyid']);
							
							$return .= "<li class='eachLine' data-id='{$srno}'> 
											<div class='strMta left'>{$rows['storylineid']}</div> 
											<div class='strLine left'>
												<p>{$rows['storyline']}</p>
												<div class='lineMta'>
													<div class='lineMtaItemOne hide'><span class='timeagoIcon'></span><abbr class='timeago' title='{$showTime}'>{$showTime}</abbr></div>
												</div>
											</div> 
											<div class='strInfo left'> ";
												$avatarPath = userimage($rows['crtdbyid'],'small');
												$imageurl = "index.php?page=". strtolower(getUsername($rows['crtdbyid']));
														
												$return .= "<a href='".$imageurl."' class='avatar'><img src='{$avatarPath}' width='40' height='40' /></a> 
												<a href='{$imageurl}' class='username'><span class='left'>".getName($rows['crtdbyid'])."</span></a> 
												<div class='rep left'> 
													<span class='bronze'></span><span class='credits'>{$credits}</span> 
												</div> 
											</div> 
											<div class='clear'></div> 
										</li>";
								$srno++;
							}
		$return .= "</ul>";
		if($showmore == 1) { 
			
			$return .="<span class='readMore'><a href='index.php?page=story&id={$k}&title=".getStoryTitle($k)."'>Read {$linecount} More ";
			if($linecount == 1) {
				$return .= "line";
			} else {
				$return .= "lines";
			}
			$return .= "</a></span>";
			
		} else { $return .="<span class='readMore'><a href='index.php?page=story&id={$k}&title=".getStoryTitle($k)."'>Post a Twist</a></span>"; }
		$return .= '</div><div class="bottomShadow"></div>';
    }        

echo $return;
?>